<?php

	// +----------------------------------------------------------------------
	// | 菜单控制器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\admin;

	use app\model\admin\Menu as MenuModel;
	use yicmf\builder\Edit;
	use yicmf\builder\Table;
	use think\facade\Cache;
	use think\facade\Db;
	use app\event\ucenter\User;

	class Menu extends Common
	{
		protected function initialize()
		{
			parent::initialize();
		}

		public function _filter()
		{
			$map[] = ['pid', '=', $this->request->get('pid', 0)];
			return $map;
		}

		public function index()
		{
			// 读取规则列表
			$tops = MenuModel::where('status',1)->where('pid',0)->order('sort asc')->select();
			$lists = [];

			foreach ($tops as $index => $top) {
				$temp =  $top->toArray();
				$temp['title'] = '<i class="layui-icon layui-icon-'.$temp['icon'].'"></i> &nbsp;&nbsp;'.	$temp['title'] ;
				$lists[] = $temp;
				$child =  MenuModel::where('status',1)->where('pid',$top['id'])->order('group asc,sort asc')->select();
				foreach ($child as $index => $item) {
					$temp =  $item->toArray();
					$temp['title'] = ' &nbsp;&nbsp;<i class="layui-icon layui-icon-'.$item['icon'].' &nbsp;&nbsp;"></i>|--'.	$temp['title'] ;
					$lists[] = $temp;
				}
			}
			// 显示页面
			$builder = new Table();
			$builder
				->order('id DESC')
//				->suggest('一般无需更改')
//				->searchText('title', '标题', '标题')
				->buttonUpdate(url('update', ['pid' => $this->request->get('pid')]))
				->keyId()
//				->keyLink('title', '标题', 'index?pid={$id}')
				->keyText('title', '标题')
				->keyText('group', '分组')
				->keyText('module', '所属模块')
				->keyText('url', 'URL')
				->keyText('sort', '排序')
				->keyText('sort', '排序')
//				->keyText('icon', '图标')
//				->keyBool('is_dev', '开发模式')
//				->keyBool('hide', '隐藏')
//				->keyTime('update_time', '最近更新')
				->keyStatus()
				->data($lists,false)
				->actionUpdate('update?id={$id}')
				->actionRestore('restore?id={$id}')
				->actionDelete('del?id={$id}');
			return $builder->fetch();
		}

		public function update()
		{
			if ($this->request->isPost()) {
				parent::_update();
			} else {
				$menus = Db::name('Menu')->select();
				$tree = new \app\common\model\Tree();
				$menus = $tree->toFormatTree($menus);
				$menus = array_merge([0 => ['id' => 0, 'title_show' => '顶级菜单']], $menus);
				$menu = [];
				foreach ($menus as $value) {
					$menu[$value['id']] = $value['title_show'];
				}
				//判断是否为编辑模式
				$isEdit = $this->request->has('id', 'get') ? true : false;
				if ($isEdit) {
					$data = $this->model->find($this->request->get('id'));
				} else {
					$data['pid'] = $this->request->get('pid', 0);
				}
				$builder = new Edit();
				$builder->title($isEdit ? '编辑菜单' : '添加菜单')
					->keyText('title', '标题', '用于后台显示的配置标题')
					->keyText('icon', '小图标', '用于显示在菜单左侧，不填则不显示')
					->keyNumber('sort', '排序', '用于分组显示的顺序')
					->keyText('url', '链接', 'Url::build解析的URL或者外链')
					->keySelect('pid', '上级菜单', $menu, '所属的上级菜单')
					->keyText('group', '分组', '用于左侧分组二级菜单')
					->keySwitch('hide', '是否隐藏')
					->keySwitch('is_dev', '仅开发者模式可见')
					->keyText('tip', '说明', '菜单详细说明')
					->data($data)
					->buttonSubmit('', '保存')->buttonClose();
				return $builder->fetch();
			}
		}

		/**
		 * 删除后台菜单.
		 */
		public function del()
		{
			if (!User::isAdministrator($this->user)) {
				$this->_returnError( '您没有编辑菜单的权限!');
			} else {
				if (!$this->request->has('id', 'get')) {
					$this->_returnError( '请选择要操作的数据!');
				}
				$menu = $this->model->get($this->request->get('id'));
				if ($menu) {
					$menu->status = -2;
					$menu->save();
					Cache::rm('system_menus_lists');
					// 记录行为
					Action::log('menu_update', $menu);
					$this->_returnSuccess( '删除菜单成功！', '', false);
				} else {
					$this->_returnError( '删除菜单失败！', '', false);
				}
			}
		}
	}
